package com.chhuang.huaao.controller;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.chhuang.core.bean.QueryBean;
import com.chhuang.huaao.bean.HaFeedbackQueryBean;
import com.chhuang.huaao.po.HaFeedback;
import com.chhuang.huaao.service.IHaFeedbackService;
import com.chhuang.system.consts.SysConstants;
import com.chhuang.utils.Assert;

@Controller
@RequestMapping("/system/feedback")
public class HaFeedbackController {
	private static final Log log = LogFactory.getLog(HaFeedbackController.class);
	
	@Autowired
	private IHaFeedbackService haFeedbackService;

	/**
	 * 根据反馈id获取反馈信息
	 * @param feedbackId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/findFeedbackById",method=RequestMethod.POST)
	@ResponseBody
	public Object findFeedbackById(String feedbackId) throws Exception{
		JSONObject json = new JSONObject();
		
		log.debug("反馈ID："+feedbackId);
		if(Assert.isValidString(feedbackId)){
			HaFeedback feedback = haFeedbackService.findByPrimaryKey(feedbackId);
			if(feedback!=null){
				json.put(SysConstants.Status.SUCCESS, true);
				json.put(SysConstants.Status.RESULT, feedback);
			}else{
				json.put(SysConstants.Status.SUCCESS, false);
				json.put(SysConstants.Status.RESULT, "反馈不存在");
			}
		}else{
			json.put(SysConstants.Status.SUCCESS, false);
			json.put(SysConstants.Status.RESULT, "反馈ID不为空");
		}
		
		log.debug(json.toString());
		return json;
	}
	
	/**
	 * 获取反馈列表
	 * @param feedbackQueryBean
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/getList",method=RequestMethod.POST)
	@ResponseBody
	public Object getList(@RequestBody HaFeedbackQueryBean feedbackQueryBean) throws Exception{
		log.debug(feedbackQueryBean.getPageSize()+";"+feedbackQueryBean.getPageNum()
				+";"+feedbackQueryBean.getOrderBy()+";"+feedbackQueryBean.isDesc()
				+";"+feedbackQueryBean.isOr());
		
		QueryBean<HaFeedback> queryBean = haFeedbackService.seniorQuery(feedbackQueryBean);
		
		return queryBean;
	}
	
	/**
	 * 删除反馈信息
	 * @param ids
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/delFeedbacks/{ids}",method=RequestMethod.DELETE)
	@ResponseBody
	public Object delFeedbacks(@PathVariable String ids) throws Exception{
		JSONObject json = new JSONObject();
		if(Assert.isValidString(ids)){
			String[] feedbackIds = ids.split(SysConstants.Symbol.COMMA);
			if(Assert.isValidArray(feedbackIds)){
				List<String> list = Arrays.asList(feedbackIds);
				haFeedbackService.deleteByPrimaryKeys(list);
				json.put(SysConstants.Status.SUCCESS, true);
			}
			json.put(SysConstants.Status.SUCCESS, true);
		}
		log.debug(ids+":::"+json.toString());
		return json;
	}

}
